﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyBuildingCMS.BLL.CoreEngine;

namespace EasyBuildingCMS.BuildingBlocks.ContentEdit
{
    public partial class ContentManagerUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            int id = (int)(BLL.Helper.ToInt(HttpContext.Current.Request.QueryString["buildingBlockInstanceID"]));
            BuildingBlockInstanceRepository buildingBlockInstanceRepository = new BuildingBlockInstanceRepository();
            BuildingBlockInstance buildingBlockInstance = buildingBlockInstanceRepository.GetBuildingBlockInstanceID(id);
                
            EditableHTMLContentRepository editableHTMLContentRepository = new EditableHTMLContentRepository();
            EditableHTMLContent editableHTMLContent = editableHTMLContentRepository.GetHTMLContentByBuildingBlockInstanceID(id);
            if (editableHTMLContent == null) 
            {
                editableHTMLContentRepository.InsertHTMLContent(0, "", buildingBlockInstance.BuildingBlockInstanceFriendlyName, id, "");
                editableHTMLContent = editableHTMLContentRepository.GetHTMLContentByBuildingBlockInstanceID(id);
            }

            this.htmlEditor.Value = editableHTMLContent.HTMLContent;

            if (!IsPostBack)
            {
                this.FriendlyNameTextBox.Text = editableHTMLContent.HTMLContentFriendlyName;
                this.CssClassTextBox.Text = editableHTMLContent.CssClass;
            }
               
        }

        protected void HTMLEditorLinkButton_Click(object sender, EventArgs e)
        {
            string value = this.htmlEditor.Value;
            int id = (int)(BLL.Helper.ToInt(HttpContext.Current.Request.QueryString["buildingBlockInstanceID"]));
            EditableHTMLContentRepository editableHTMLContentRepository = new EditableHTMLContentRepository();
            editableHTMLContentRepository.UpdateHTMLContent(id, value);

            BuildingBlockInstanceRepository buildingBlockInstanceRepository = new BuildingBlockInstanceRepository();
            string buildingBlockPlaceHolderName = buildingBlockInstanceRepository.GetBuildingBlockInstanceID(id).ContentPlaceHolderName;

            string script = "<script language=\"javascript\">window.opener.document.getElementById('hiddenforpostback" + buildingBlockPlaceHolderName + "').click();window.close();</script>";
            Parent.Page.RegisterStartupScript("clientScript", script);

        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            int id = (int)(BLL.Helper.ToInt(HttpContext.Current.Request.QueryString["buildingBlockInstanceID"]));
            EditableHTMLContentRepository editableHTMLContentRepository = new EditableHTMLContentRepository();
            editableHTMLContentRepository.UpdateFriendlyName(id, this.FriendlyNameTextBox.Text);
            editableHTMLContentRepository.UpdateCssClass(id, this.CssClassTextBox.Text);
        }

 
    }
}